<?php
class QuestionsController extends AppController {

	var $name = 'Questions';
	var $uses = array('Question','Language','Answer');
	var $helpers = array('Avatar','Formater','Text');
	
	var $paginate = array(
			'order'=>array('Question.id'=>'desc')
		);


	function index() {
		$this->set('title_for_layout', '问答频道');
		//$this->Question->recursive = 0;
		
		$this->set('questions', $this->paginate());
		$this->set('languages', $this->Language->find('all'));

	}
	function solved()
	{
		$this->set('questions', $this->paginate('Question',array('Question.status'=>Question::SOLVED)));
		$this->render('index');
	}
	function unsolved()
	{
		$this->set('questions', $this->paginate('Question',array('Question.status'=>Question::UNSOLVED)));
		$this->render('index');
	}
	function search()
	{
		$this->set('title_for_layout', "搜索问答频道");
		$this->set('languages', $this->Language->find('all'));
		$conditions=array();
		if (!empty($this->params['url']['lan'])){
			$conditions[' Question.language_id'] = $this->params['url']['lan'];
		}
		if (!empty($this->params['url']['q'])){
			$q = $this->params['url']['q'];
			$conditions['Question.title LIKE '] = "%{$q}%";
		}
		$this->set('questions',$this->paginate('Question',$conditions));        
	}

	function view($id = null) {
		if (!$id) {
			$this->cakeError("not_found");
		}
		$question = $this->Question->read(null,$id);
		$this->set('solved', Question::is_solved($id));
		$this->set('title_for_layout', $question['Question']['title']);
		$this->set('Answers', $this->paginate('Answer',array('Answer.question_id'=>$id)));
		$this->set('question', $question);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Question->create();
			if ($this->Question->save($this->data)) {
				$this->Session->setFlash(__('The question has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
			}
		}
		$languages = $this->Question->Language->find('list');
		$this->set(compact('languages'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid question', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Question->save($this->data)) {
				$this->Session->setFlash(__('The question has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Question->read(null, $id);
		}
		$users = $this->Question->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for question', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Question->delete($id)) {
			$this->Session->setFlash(__('Question deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Question was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function admin_index()
	{
		# code...
	}
}
?>